﻿/*
 * Author:xmfdsh 11届网络部--邓世豪
 * DataTime：2014-6-9
 * Copyright © 2014 大学生勤工助学服务队 Powered by xmfdsh & 四小狼工作室.
 * 版权所有，违者必究
 */
using QGZX.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace QGZX.Logic.OA
{
    public class SettingController : Controller
    {
        #region 部门设置管理页面 + ActionResult Department()
        /// <summary>
        /// 部门设置管理页面
        /// </summary>
        /// <returns></returns>
        public ActionResult Department()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;

            List<MODEL.qgoa_department> list = OperateContext.Current.BLLSession.Iqgoa_departmentBLL.GetListBy(x => x.Id != null);
            ViewBag.List = list;
            return View();
        } 
        #endregion

        #region 添加部门页面 + CreateDepartment()
        /// <summary>
        /// 添加部门页面
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateDepartment()
        {
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            List<MODEL.qgoa_department> list = OperateContext.Current.BLLSession.Iqgoa_departmentBLL.GetListBy(x => x.Id != null);
            ViewBag.list = list;
            return View();
        } 
        #endregion

        #region 编辑部门页面 + EditDepartment()
        /// <summary>
        /// 编辑部门页面
        /// </summary>
        /// <returns></returns>
        public ActionResult EditDepartment()
        {
            int Id = Convert.ToInt32(Request["id"]);
            MODEL.qgoa_department department = OperateContext.Current.BLLSession.Iqgoa_departmentBLL.GetListBy(x => x.Id == Id).FirstOrDefault();
            ViewBag.department = department;
            MODEL.qgzx_config model = OperateContext.Current.BLLSession.Iqgzx_configBLL.GetListBy(x => x.SystemName != null).FirstOrDefault();
            ViewBag.configModel = model;
            return View();
        } 
        #endregion

        #region 部门处理方法 集合 + ActionDepartment()
        /// <summary>
        /// 部门处理方法 集合
        /// </summary>
        /// <returns></returns>
        public ActionResult ActionDepartment()
        {
            string dowhat = Request.Form["dowhat"];

            if (dowhat == "create_department")
            {
                string Name = Request.Form["Name"];
                MODEL.qgoa_department department = new MODEL.qgoa_department() { Name = Name };
                if (OperateContext.Current.BLLSession.Iqgoa_departmentBLL.Add(department) > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                else
                {
                    return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
                }
            }

            if (dowhat == "delete_department")
            {
                int Id = Convert.ToInt32(Request.Form["Id"]);

                if (OperateContext.Current.BLLSession.Iqgoa_departmentBLL.DelBy(x => x.Id == Id) > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                else
                {
                    return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
                }
            }
            if (dowhat == "update_department")
            {
                int Id = Convert.ToInt32(Request.Form["Id"]);
                string Name = Request.Form["Name"];
                string Remark = Request.Form["Remark"];
                MODEL.qgoa_department model = OperateContext.Current.BLLSession.Iqgoa_departmentBLL.GetListBy(x => x.Id == Id).FirstOrDefault();
                model.Name = Name;
                model.Remark = Remark;

                if (OperateContext.Current.BLLSession.Iqgoa_departmentBLL.ModifyBy(model, x => x.Id == Id, "Name", "Remark") > 0)
                {
                    return OperateContext.Current.RedirectAjax("ok", "操作成功", null, "");
                }
                else
                {
                    return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
                }
            }
            return OperateContext.Current.RedirectAjax("err", "操作失败", null, "");
        } 
        #endregion
    }
}
